﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WD.Db.Contract;

namespace WD.Web.MVC
{
    public class ApiResult
    {
        /// <summary>
        /// 操作成功
        /// </summary> 
        /// <param name="msg"></param>
        /// <returns></returns>
        public JObject Suc(string msg)
        {
            var result = new ApiResult
            {
                statusCode = StatusCode.成功,
                message = msg,
                Data = Data
            };
            var strJson = result.ToJson();
            return JObject.Parse(strJson);
        }

        public JObject Err(string msg, string navTabId = "")
        {
            var result = new ApiResult
            {
                statusCode = StatusCode.错误,
                message = msg
            };
            var strJson = result.ToJson();
            return JObject.Parse(strJson);
        }

        /// <summary>
        /// 消息代码：200：成功
        /// </summary>
        public StatusCode statusCode { get; set; } //301 登录超时  300错误
        public HandelCode HandelCode { get; set; }  //扩展后 需要在AJAXDONE中根据CODE实现JS函数
        /// <summary>
        /// 消息内容
        /// </summary>
        public string message { get; set; }
        /// <summary>
        /// 数据
        /// </summary>
        public object Data { get; set; } //存放数据

        public string ToJson()
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(this);
        }

        public string ToJson(StatusCode statusCode, string message, string Data = "", dynamic Attr = null)
        {
            this.statusCode = statusCode;
            this.message = message;
            this.Data = Data;
            return Newtonsoft.Json.JsonConvert.SerializeObject(this);
        }
    }
}